package com.eattravel.controller;

import com.eattravel.dto.PageInfoResult;
import com.eattravel.pojo.Order;
import com.eattravel.service.OrderService;
import com.eattravel.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zx
 * @date 2019.9.22
 * 描述 订单相关
 */
@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 通过用户Id查询订单
     * @param userId
     * @return
     */
    @RequestMapping("findOrderByUserId")
    @ResponseBody
    public ResultData findOrderByUserId(String userId){
        ResultData  resultData = new ResultData();
        List<Order> orderList=orderService.findOrdersByUserId(userId);
        if(null==orderList||orderList.size()==0){
            resultData.setCode(3);
            resultData.setMessage("对不起，没有需要付款的订单");
        }else {
            resultData.setCode(0);
            resultData.setData(orderList);
        }
        return resultData;

    }

    /**
     * 通过用户Id查询订单 分页的方法
     * @param userId
     * @return
     */
    @RequestMapping("findOrderByUserIdAndPage")
    @ResponseBody
    public PageInfoResult<Order> findOrderByUserId(String userId, PageInfoResult pageParameter){
        pageParameter.setPageSize(5);
        /*  ResultData  resultData = new ResultData();*/
        PageInfoResult<Order> orderList=orderService.findOrdersByUserId(userId,pageParameter);

        return orderList;


    }

    @RequestMapping("deleteOrderById")
    @ResponseBody
    public ResultData deleteOrderById(String orderId){
        ResultData  resultData = new ResultData();
        boolean flag= orderService.deleteOrderById(orderId);

        if(flag){
            resultData.setCode(0);
            resultData.setMessage("删除成功");

            return resultData;

        }


        resultData.setCode(1);
        resultData.setMessage("删除失败，请刷新重试");

        return resultData;
    }





    /**
     * 通过订单Id查询订单
     * @param userId
     * @return
     */
    @RequestMapping("findOrderById")
    @ResponseBody
    public Order findOrderById(String userId,String orderId){
        ResultData  resultData = new ResultData();
        List<Order> orderList=orderService.findOrdersByUserId(userId);

        for(int i=0;i<orderList.size();i++){
            if(orderList.get(i).getOrderId().equals(orderId)){
                return orderList.get(i);
            }

        }

        return null;

    }



    /**
     * 通过用户Id查询订单 分页的方法
     * @param userId
     * @return
     */
    @RequestMapping("findOrderByUserIdAndPage1")
    @ResponseBody
    public PageInfoResult<Order> findOrderByUserId1(String userId, PageInfoResult pageParameter){
        pageParameter.setPageSize(3);
        /*  ResultData  resultData = new ResultData();*/
        PageInfoResult<Order> orderList=orderService.findOrdersByUserId(userId,pageParameter);

        return orderList;


    }




}
